package com.sinux.mms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinux.mms.entity.Member;
import com.sinux.mms.service.MemberServiceI;


/**
 * 会员管理控制层
 * @author w.g
 * @date 2017-10-17 16:08:06
 */
@Controller
public class MemberController {
	
	@Autowired
	private MemberServiceI memberService;
	
	/**
	 * 查询全部会员
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/membertab")
	public Map<String, Object> memberTab(HttpServletRequest request, HttpServletResponse response, Model model){
		Map<String, Object> map=new HashMap<String, Object>();
		int offset =Integer.valueOf(request.getParameter("offset")); //当前页第一条数据(包含)  
	    int limit = Integer.valueOf(request.getParameter("limit")); //每页多少条数据  
		//页数=当前页第一条数据/每页多少条数据 +1
	    List<Member> members=memberService.findMemberPageing(offset/limit+1,limit);
		int total=memberService.findAllMemberList().size();
		map.put("rows", members);
		map.put("total", total);
		return map;
		
	}
	
	/**
	 * 添加会员
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addmember")
	public Map<String,Object> addMember(HttpServletRequest request, HttpServletResponse response, Member model){
		Map<String, Object> map=new HashMap<String, Object>();
		memberService.addMember(model);
		map.put("msg", "成功");
		return map;
	}
	
	/**
	 * 模糊查询会员
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getmembers")
	public Map<String,Object> getMembers(HttpServletRequest request, HttpServletResponse response, Member model){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Member> members=memberService.findMemberList(model.getTemp1());
		map.put("data", members);
		map.put("msg", "成功");
		return map;
	}
	/**
	 * 根据路径加载页面
	 * @return
	 */
	@RequestMapping("/testmetab")
	public String test(){
		return "modules/member/memberTab";
	}
	
	@RequestMapping("/member_edit")
	public String pageToMemberEdit() {
		return "modules/member/member_edit";
	}

}
